package com.cuz.daileetcode.bat100;

public class 分发糖果 {
    public static void main(String[] args) {
        int[] ints = {1, 3, 2, 2, 1};
        System.out.println(candy(ints));
    }

    //分糖果进阶
    //相同相邻得分的人必须一样多
    private static int solution4(int[] ratings) {
        if (ratings == null || ratings.length == 0) {
            return 0;
        }
        if (ratings.length == 1) {
            return 1;
        }
        int len = ratings.length;
        int[] left = new int[len];
        left[0] = 1;
        int[] right = new int[len];
        right[0] = 1;
        for (int i = 1; i < len; i++) {
            if (ratings[i] > ratings[i - 1]) {
                left[i] = left[i - 1] + 1;
            } else if (ratings[i] == ratings[i - 1]) {
                left[i] = left[i - 1];
            } else {
                left[i] = 1;
            }
        }
        for (int i = len - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                right[i] = right[i + 1] + 1;
            } else if (ratings[i] == ratings[i + 1]) {
                right[i] = right[i + 1];
            } else {
                right[i] = 1;
            }
        }
        int ans = 0;
        for (int i = 0; i < len; i++) {
            ans += Math.max(left[i], right[i]);
        }
        return ans;
    }

    public static int candy(int[] ratings) {
        int n = ratings.length;
        int ret = 1;
        int inc = 1, dec = 0, pre = 1;
        for (int i = 1; i < n; i++) {
            if (ratings[i] >= ratings[i - 1]) {
                dec = 0;
                pre = ratings[i] == ratings[i - 1] ? 1 : pre + 1;
                ret += pre;
                inc = pre;
            } else {
                dec++;
                if (dec == inc) {
                    dec++;
                }
                ret += dec;
                pre = 1;
            }
        }

        return ret;
    }

    private static int solution2(int[] ratings) {
        if (ratings == null || ratings.length == 0) {
            return 0;
        }
        if (ratings.length == 1) {
            return 1;
        }
        int len = ratings.length;
        int[] left = new int[len];
        left[0] = 1;
        int ans = 0;
        for (int i = 1; i < len; i++) {
            left[i] = ratings[i] <= ratings[i - 1] ? 1 : left[i - 1] + 1;
        }
        //最右边孩子最起码需要分配left[len - 1] 因为他右边没有人了，不可以让他的糖果更多了
        // 【1 ，2 ，4】  对于这个4 他就是要分配3 除非 4右边还有人【1，2，4，3，2，1】
        //这个时候4不是最右边的 违背了上面的假设
        ans += left[len - 1];
        int right = 1;
        for (int i = len - 2; i >= 0; i--) {
            // 倒第二个大于倒第一个的时候 这个分支也对
            //1 2 3 3 2 倒第一个 糖果为1 （从左到右分配left[4]=1）
            //那么到第二个为2 right++
            if (ratings[i] > ratings[i + 1]) {
                right++;
            } else {
                right = 1;
            }
            ans += Math.max(left[i], right);
        }
        return ans;
    }

    public static int solution1(int[] ratings) {
        if (ratings == null || ratings.length == 0) {
            return 0;
        }
        if (ratings.length == 1) {
            return 1;
        }
        int len = ratings.length;
        int[] left = new int[len];
        left[0] = 1;
        int[] right = new int[len];
        right[len - 1] = 1;
        int ans = 0;
        for (int i = 1; i < len; i++) {
            left[i] = ratings[i] <= ratings[i - 1] ? 1 : left[i - 1] + 1;
        }
        for (int i = len - 2; i >= 0; i--) {
            right[i] = ratings[i] <= ratings[i + 1] ? 1 : right[i + 1] + 1;
        }
        for (int i = 0; i < len; i++) {
            ans += Math.max(left[i], right[i]);
        }
        return ans;
    }
}
